package com.liang.maogouyuyin.utils.lzy;

import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

public class TouchUtils {

    public static void onTouch(View v) {
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true); // 禁止父视图拦截触摸事件

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        scaleView(v, 0.9f, 0.9f, 100);
                        return true; // 消费触摸事件
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        scaleView(v, 1f, 1f, 100);
                        v.performClick(); // 触发点击事件
                        return true; // 消费触摸事件
                }
                return false;
            }
        });
    }

    public static void onTouchISFalse(View v) {
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        scaleView(v, 0.9f, 0.9f, 100);
                        return false; // 消费触摸事件
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        scaleView(v, 1f, 1f, 100);
                        return false; // 消费触摸事件
                }
                return false;
            }
        });
    }

    public static void onTouchISOnLong(View v) {
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        scaleView(v, 0.9f, 0.9f, 100);
                        return false; // 消费触摸事件
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        scaleView(v, 1f, 1f, 100);
                        return false; // 消费触摸事件
                }
                return false;
            }
        });
    }


    private static void scaleView(View v, float scaleX, float scaleY, int duration) {

        ScaleAnimation scaleAnimation = new ScaleAnimation(
                1f, scaleX, 1f, scaleY,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setDuration(duration);
        v.startAnimation(scaleAnimation);
    }
}
